package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

import com.ruoyi.system.domain.Sumvo;
import com.ruoyi.system.mapper.SysUserxqMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.UserScoreMapper;
import com.ruoyi.system.service.IUserScoreService;

/**
 * 用户得分Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-18
 */
@Service
public class UserScoreServiceImpl implements IUserScoreService {
    @Autowired
    private UserScoreMapper userScoreMapper;


    @Autowired
    private SysUserxqMapper sysUserxqMapper;

    /**
     * 按季度和月份查询基层员工每项能力测评或潜力与价值观
     */
    @Override
    public List<Map> selectUserScoreByUserId2(Map map) {
        //能力测评
        Double score02 = 0.0;
        Double score03 = 0.0;
        Double score04 = 0.0;
        Double score05 = 0.0;
        Double score06 = 0.0;
        //素质测评
        Double score08 = 0.0;
        Double score09 = 0.0;
        Double score10 = 0.0;
        Double score11 = 0.0;
        Double score12 = 0.0;

        Integer parentId1 = (Integer) map.get("parentId");
        //查询所有用户测评数据
        List<Map> voss = userScoreMapper.selectUserScoreByUserId(map);


        //遍历查出的三个月一季度的数据
        for (Map map1 : voss) {
            Integer category = (Integer) map1.get("category");//分类
            Double scores = (Double) map1.get("score");//分数
            //分类一样的情况下相加再出三算出平均分再判断
            if (parentId1 == 1) {
                //能力测评
                if (category == 2) {
                    score02 += scores;
                } else if (category == 3) {
                    score03 += scores;
                } else if (category == 4) {
                    score04 += scores;
                } else if (category == 5) {
                    score05 += scores;
                } else if (category == 6) {
                    score06 += scores;
                }

            } else if (parentId1 == 7) {
                //素质评分
                if (category == 8) {
                    score08 += scores;
                } else if (category == 9) {
                    score09 += scores;
                } else if (category == 10) {
                    score10 += scores;
                } else if (category == 11) {
                    score11 += scores;
                } else if (category == 12) {
                    score12 += scores;
                }
            }
        }
        // 每季度的每项平均分
        Double v = userScoreMapper.selectBygcjcsum(map);
        if (v == 0.0) {
            return null;
        }
// 能力测评的平均分
        BigDecimal bd02 = BigDecimal.valueOf(score02 / v).setScale(2, RoundingMode.HALF_UP);
        BigDecimal bd03 = BigDecimal.valueOf(score03 / v).setScale(2, RoundingMode.HALF_UP);
        BigDecimal bd04 = BigDecimal.valueOf(score04 / v).setScale(2, RoundingMode.HALF_UP);
        BigDecimal bd05 = BigDecimal.valueOf(score05 / v).setScale(2, RoundingMode.HALF_UP);
        BigDecimal bd06 = BigDecimal.valueOf(score06 / v).setScale(2, RoundingMode.HALF_UP);

        Double avg02 = bd02.doubleValue();
        Double avg03 = bd03.doubleValue();
        Double avg04 = bd04.doubleValue();
        Double avg05 = bd05.doubleValue();
        Double avg06 = bd06.doubleValue();
// 素质测评的平均分
        BigDecimal bd08 = BigDecimal.valueOf(score08 / v).setScale(2, RoundingMode.HALF_UP);
        BigDecimal bd09 = BigDecimal.valueOf(score09 / v).setScale(2, RoundingMode.HALF_UP);
        BigDecimal bd10 = BigDecimal.valueOf(score10 / v).setScale(2, RoundingMode.HALF_UP);
        BigDecimal bd11 = BigDecimal.valueOf(score11 / v).setScale(2, RoundingMode.HALF_UP);
        BigDecimal bd12 = BigDecimal.valueOf(score12 / v).setScale(2, RoundingMode.HALF_UP);

        Double avg08 = bd08.doubleValue();
        Double avg09 = bd09.doubleValue();
        Double avg10 = bd10.doubleValue();
        Double avg11 = bd11.doubleValue();
        Double avg12 = bd12.doubleValue();
        //再次遍历将一个季度数据加入
        List<Map> list1 = new ArrayList<>();
        //查询所有评价的详情
        List<Map> vose = userScoreMapper.selectUserScoreByUserId2(parentId1);

        for (Map maps : vose) {
//            判断catgory为2时将平均数插进去然后得出对应分数的解析详情
            Integer category = (Integer) maps.get("category");//分类

//            能力测评，
            if (parentId1 == 1) {
                if (category == 2) {
                    maps.put("score", avg02);
                } else if (category == 3) {
                    maps.put("score", avg03);
                } else if (category == 4) {
                    maps.put("score", avg04);
                } else if (category == 5) {
                    maps.put("score", avg05);
                } else if (category == 6) {
                    maps.put("score", avg06);
                }
            }

            if (parentId1 == 7) {
                if (category == 8) {
                    maps.put("score", avg08);
                } else if (category == 9) {
                    maps.put("score", avg09);
                } else if (category == 10) {
                    maps.put("score", avg10);
                } else if (category == 11) {
                    maps.put("score", avg11);
                } else if (category == 12) {
                    maps.put("score", avg12);
                }
            }
            //将存入平均分的数据存入list1中
            list1.add(maps);
        }

        //  定义最后一个集合将最终数据放进去
        List<Map> list2 = new ArrayList<>();
        //判断分数的强弱
        for (Map map2 : list1) {
            Double score = (Double) map2.get("score");
            Integer grade = (Integer) map2.get("grade");//分类
            if (score > 0 && score < 10 && grade == 3) {
                list2.add(map2);
            } else if (score >= 10 && score < 15 && grade == 2) {
                list2.add(map2);
            } else if (score >= 15 && score <= 20 && grade == 1) {
                list2.add(map2);
            }
        }
        return list2;
    }

    @Override
    public Map selectUserIdByNickname(Integer userId) {
        return userScoreMapper.selectUserIdByNickname(userId);
    }

    @Override
    public List<Map> selectUserScoreByUserId3(Map map) {
        return userScoreMapper.selectallxiang(map);
    }

    @Override
    public List<Sumvo> selectUserScoreByUserId4(Map map) {
        List<Sumvo> pp = new ArrayList<>();
        //能力测评总分
        map.put("parentId", 1);
        List<Double> map1 = sysUserxqMapper.selectByjcCount1(map);

        //素质测评总分
        map.put("parentId", 7);
        List<Double> map2 = sysUserxqMapper.selectByjcCount1(map);

        //合计总分
        map.put("parentId", 0);
        List<Double> mapp = sysUserxqMapper.selectByjcCount1(map);
        //评分人姓名
        List<String> s = userScoreMapper.selectByjcName(map);

        //评分人id
        List<Long> userId = userScoreMapper.selectByjcCount1(map);
        for (int i = 0; i < s.size(); i++) {
            Sumvo sumvo = new Sumvo(
                    userId.get(i),
                    s.get(i),
                    map1.get(i),
                    map2.get(i),
                    mapp.get(i).doubleValue()
            );
            pp.add(sumvo);
        }

        return pp;
    }

    //根据id查询权限职位
    @Override
    public List<Map> selectUserId(List<Long> userIds) {
        return userScoreMapper.selectUserId(userIds);
    }

    @Override
    public Map selectUserIdgaoceng(Long userId) {
        return userScoreMapper.selectUserIdgaoceng(userId);
    }

    @Override
    public List<Map> selectBylist(String roleid, String deptid) {
        if (roleid != null) {
            if (roleid.equals("102")||roleid.equals("2")||roleid.equals("3")) {
                //高层查中层
                roleid = "null";
                deptid = "null";
            } else if (roleid.equals("1")) {
                //管理原查询
                roleid = "null";
                deptid="null";
            }else  if(roleid.equals("101")){
                //中层查询基础
                roleid="100";
            }else {
                roleid="500";
            }
        }
        Map map=new HashMap<>();
        map.put("roleid",roleid);
        map.put("deptid",deptid);
        return userScoreMapper.selectBylist(map);
    }


}
